package com.example.demo01.security;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;



@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    final UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/admin/**" , "/admin").hasAnyRole("ADMIN", "ROOT")


                .antMatchers("/root/**").hasAnyRole("ROOT")

                .antMatchers("/user/**","/user").hasAnyRole("USER")

                .antMatchers("/login.html","/login").permitAll()
                // 自定义登录界面
                .and().formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login")
                // 设置成功后跳转的页面
                .defaultSuccessUrl("/");

        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    @Override
    public void configure(WebSecurity web){
        web.ignoring().antMatchers("/css/**","/img/**","/js/**","/assets/**");
    }

}
